<template>
  <div class="mod-config">
    <!-- 院级 头部 导出区域 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="项目标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:college:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('sys:college:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <!-- <el-button type="primary" size="small" @click="submitNext()">提交给校级</el-button> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="showExcel = true" :disabled="excelData.length == 0">{{ '导出' }}</el-button>
      </el-form-item>
      <el-form-item>

        <el-dialog :visible.sync="showExcel" modal-append-to-body append-to-body width="50%">
          <div>
            <h3>输入excel基本信息</h3>
            <el-form class="excelForm" :label-position="'left'">
              <el-form-item label="excel表标题:">
                <el-input size="small" placeholder="表格标题" style="width:160px" v-model="excelTitle"></el-input>
              </el-form-item>
              <el-form-item label="excel文件名:">
                <el-input size="small" placeholder="文件名" style="width:160px" v-model="excelFName">
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-radio-group v-model="excelType">
              <el-radio-button :label="0">{{ '导出项目信息' }}</el-radio-button>
              <el-radio-button :label="1">{{ '导出项目成员信息' }}</el-radio-button>
            </el-radio-group>
          </div>
          <div v-if="excelType == 0">
            <h3>选要导出的字段</h3>
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkExcelAll"
              @change="handleCheckAllChange">全选</el-checkbox>
            <el-checkbox-group v-model="checked_key" @change="handleCheckedKeyChange"
              style="display:flex ; align-items: center;flex-wrap: wrap; justify-content: center;">
              <el-checkbox v-for="k in handleJson_fields" :label="k" :key="k.key">{{ k.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-else-if="excelType == 1">
            <h3>选要导出的字段</h3>
            <el-checkbox :indeterminate="isIndeterminate1" v-model="checkExcelAll1"
              @change="handleCheckAllChange1">全选</el-checkbox>
            <el-checkbox-group v-model="checked_key1" @change="handleCheckedKeyChange1"
              style="display:flex ; align-items: center;flex-wrap: wrap; justify-content: center;">
              <el-checkbox v-for="k in handleMember_fields" :label="k" :key="k.key">{{ k.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <span slot="footer" class="dialog-footer" style="display: flex;justify-content: flex-end;">
            <el-button @click="showExcel = false">取 消</el-button>
            <!-- <el-button type="primary" @click="showExcel = false">确 定</el-button> -->
            <download-excel :data="excelType == 0 ? excelData : memberData" :fields="excelType == 0 ? header : header1"
              :header="excelTitle" :name="excelFName" :before-generate='beforeGenerate'>
              <el-button
                v-if="(excelTitle != '' && excelFName != '' && checked_key.length != 0) || (excelTitle != '' && excelFName != '' && checked_key1.length != 0)"
                icon="el-icon-download" style="margin: 0 10px;" type="primary">
                {{ "立即导出" }}
              </el-button>
              <el-tag v-else disable-transitions style="margin: 0 0 0 5px; height: 36px;line-height: 36px;">{{
                '填完上面的东西显示导出按钮' }}</el-tag>
            </download-excel>
          </span>
        </el-dialog>

      </el-form-item>
    </el-form>
    <!-- 表格主体 -->
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="projectType" header-align="center" align="center" label="比赛类型" width="80" fixed="left">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="项目类型" fixed="left">
      </el-table-column>
      <el-table-column prop="college" header-align="center" align="center" label="所属学院" width="125" fixed="left">
      </el-table-column>

      <el-table-column prop="title" header-align="center" align="center" label="项目标题" width="250">
      </el-table-column>


      <el-table-column prop="outline" header-align="center" align="center" label="项目概要" width="250">
        <template slot-scope="scope">
          <el-tooltip content="查看概要" :open-delay="500">
            <div @click="showOutlineInfo(scope.row.outline)"
              style="display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 3;overflow: hidden;">
              {{ scope.row.outline }}
            </div>
          </el-tooltip>
          <el-dialog title="项目概要" append-to-body :visible.sync="showOutline" width="30%">
            <span>{{ outline }}</span>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="showOutline = false">确 定</el-button>
            </span>
          </el-dialog>
        </template>
      </el-table-column>
      <el-table-column prop="teacherName" header-align="center" align="center" label="指导老师">
      </el-table-column>
      <el-table-column prop="leader" header-align="center" align="center" label="负责人">
      </el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="负责人联系方式">
      </el-table-column>
      <el-table-column prop="average" header-align="center" align="center" label="平均分">
        <template slot-scope="scope">
          <div>{{ scope.row.average ? scope.row.average : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="peopleNumber" header-align="center" align="center" label="评分总人数">
        <template slot-scope="scope">
          <div>{{ scope.row.peopleNumber ? scope.row.peopleNumber : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="noScore" header-align="center" align="center" label="未评分专家名单" width="125">
        <template slot-scope="scope">
          <div v-if="scope.row.noScore.length == 0">无未评分专家</div>
          <el-dropdown v-else>
            <span style="color:#4ea1e1 ;">
              {{ scope.row.noScore.length + '位专家未评' }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(k, i) in scope.row.noScore" :key="i" disabled>{{ k }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column prop="statu" header-align="center" align="center" label="评审状态" width="120">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.statu == 1">通过</el-tag>
          <el-tag type="danger" v-else-if="scope.row.statu == 2">不通过</el-tag>
          <el-tag type="danger" v-else-if="scope.row.statu == 3">被校级打回</el-tag>
          <el-tag type="danger" v-else-if="scope.row.statu == 4">已打回给学生</el-tag>
          <!-- <el-tag type="danger" v-else-if="scope.row.statu == 3">不予立项</el-tag> -->
          <el-select v-else v-model="scope.row.statu" placeholder="请选择" @change="change(scope.row.id, scope.row.statu)">
            <el-option v-for="item in status" :key="item.id" :label="item.statu" :value="item.id">
            </el-option>
          </el-select>

        </template>


      </el-table-column>
      <el-table-column prop="ranks" header-align="center" align="center" label="院推荐意见" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.ranks" :type="scope.row.ranks === '不予立项' ? 'success' : 'danger'">
            {{ scope.row.ranks }}
          </el-tag>
          <div v-else>暂无</div>
        </template>
      </el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="text" size="small" @click="repulse(scope.row.id, scope.row.title)">打回给学生</el-button>
          </div>
          <el-button type="text" size="small" @click="toDetail(scope.row.id)">查看</el-button>
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 画重点项目 -->
    <el-dialog title="标记项目" :visible.sync="showWin" width="30%" :before-close="handleClose" :center="true">
      <span>
        <el-radio-group v-model="reviewForm.ranks" fill="#F56C6C">
          <el-radio-button label="校级"></el-radio-button>
          <el-radio-button label="省级、国家级"></el-radio-button>
          <!-- <el-radio-button label="不予立项"></el-radio-button> -->
        </el-radio-group>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitNext()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

  </div>
</template>

<script>
import AddOrUpdate from './college-add-or-update'
export default {
  data() {
    return {
      status: [{
        id: 0,
        statu: '审核中',
      }, {
        id: 1,
        statu: '通过',
      }, {
        id: 2,
        statu: '不通过',
      }, {
        id: 3,
        statu: '不予立项',
      }],
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      showExcel: false,


      json_fields: {
        '项目id': 'id',
        '项目标题': 'title',
        '项目类型': 'type',
        '比赛类型': 'projectType',
        '学院': 'college',
        '项目概要': 'outline',
        '负责人': 'leader',
        '指导老师': 'teacherName',
        // '审核材料': 'file',
        '平均分': 'average',
        // '评分人数': 'peopleNumber',
        '审核状态': 'statuString',
      },
      memberJson_fields: {
        '项目标题': 'title',
        '学院': 'college',
        '班级': 'grade',
        '姓名': 'name',
        '登录邮箱': 'mailbox',
        '手机号码': 'phone',
        '身份': 'statuString',
      },
      //已选择的字段
      checked_key: [],
      checked_key1: [],
      checkedMember_key: [],
      // 供选择的字段
      handleJson_fields: [
        {
          name: '项目标题',
          key: 'title',
        },
        {
          name: '项目类型',
          key: 'type',
        },
        {
          name: '比赛类型',
          key: 'projectType',
        },
        {
          name: '学院',
          key: 'college',
        },
        {
          name: '项目概要',
          key: 'outline',
        },
        {
          name: '指导老师',
          key: 'teacherName',
        },
        {
          name: '负责人',
          key: 'leader',
        },
        {
          name: '负责人联系方式',
          key: 'phone',
        },
        {
          name: '评分人数',
          key: 'peopleNumber',
        },
        {
          name: '院推荐意见',
          key: 'ranks',
        },
        {
          name: '平均分',
          key: 'average',
        },
        {
          name: '审核状态',
          key: 'statu',
        },
      ],
      handleMember_fields: [
        {
          name: '项目标题',
          key: 'title',
        },
        {
          name: '学院',
          key: 'college',
        },
        {
          name: '班级',
          key: 'grade',
        },
        {
          name: '姓名',
          key: 'name',
        },
        {
          name: '登录邮箱',
          key: 'mailbox',
        },
        {
          name: '手机号码',
          key: 'phone',
        },
        {
          name: '身份',
          key: 'statuString',
        }
      ],
      //导出excel全选
      checkExcelAll: false,
      //导出excel1全选
      checkExcelAll1: false,
      //控制全选框的样式
      isIndeterminate: false,
      //控制全选框1的样式
      isIndeterminate1: false,
      //excel标题
      excelTitle: '',
      //excel文件名
      excelFName: '院级项目表.xls',
      //导出excel的数据数组
      excelType: 0,//导出数据的类型，0项目信息，1成员信息
      excelData: [],
      ids: [],
      memberData: [],
      showWin: false,//项目重点窗口
      //评审提交的数据
      reviewForm: {
        ranks: '一般项目',
        id: null,
        statu: null,
      },
      //显示概要
      showOutline: false,
      //概要
      outline: '',
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
    console.log(Object.keys(this.json_fields)[0], this.json_fields);
  },
  computed: {
    excelBtnDis() {
      if ((this.excelTitle !== '') && (this.excelFName !== '') && (this.excelData.length !== 0)) return false
      return true
    },
    header() {
      let obj = {}
      for (let i = 0; i < this.checked_key.length; i++) {
        if (this.checked_key[i].key == 'statu') {
          obj[this.checked_key[i].name] = {
            field: this.checked_key[i].key,
            callback: value => {
              if (value == 0) {
                return '审核中'
              } else if (value == 1) {
                return '通过'
              } else if (value == 2) {
                return '不通过'
              }
            }
          }
        } else {
          obj[this.checked_key[i].name] = this.checked_key[i].key
        }

      }
      return obj
    },
    header1() {
      let obj = {}
      for (let i = 0; i < this.checked_key1.length; i++) {
        obj[this.checked_key1[i].name] = this.checked_key1[i].key
        // if (this.checked_key1[i].key == 'statu') {
        //   obj[this.checked_key1[i].name] = {
        //     field: this.checked_key1[i].key,
        //     callback: value => {
        //       if (value == 0) {
        //         return '审核中'
        //       } else if (value == 1) {
        //         return '通过'
        //       } else if (value == 2) {
        //         return '不通过'
        //       }
        //     }
        //   }
        // } else {
        //   obj[this.checked_key1[i].name] = this.checked_key1[i].key
        // }

      }
      return obj
    }
  },
  methods: {
    //院级打回给学生
    repulse(id, name) {
      let title = '你确定要将该项目打回吗'
      let content = '项目标题：' + name
      this.$confirm(content, title, {
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/college/refers'),
          method: 'post',
          data: this.$http.adornData({
            id,
            // statu: 3
          })
        }).then(({
          data
        }) => {
          if (data && data.code == 0) {
            this.$message.success('已打回项目')
            this.getDataList()
          } else {
            this.$message.warning(data.msg)
          }
        })
      }).catch((action) => {
        if (action == 'cancel')
          this.$message.warning('取消打回')
        this.getDataList()
      })



    },
    showOutlineInfo(text) {
      this.showOutline = true
      this.outline = text
    },
    changeExc() {
      this.excelType = !this.excelType
      console.log(this.excelType)
    },
    handleCheckAllChange(val) {
      // let arr = []
      // for (let i = 0; i < this.handleJson_fields.length; i++) {
      //   arr.push(this.handleJson_fields[i].key)
      // }
      // console.log(arr, val);
      this.checked_key = val ? this.handleJson_fields : []
      this.isIndeterminate = false
    },
    // 全选表格1
    handleCheckAllChange1(val) {
      // let arr = []
      // for (let i = 0; i < this.handleJson_fields.length; i++) {
      //   arr.push(this.handleJson_fields[i].key)
      // }
      // console.log(arr, val);
      this.checked_key1 = val ? this.handleMember_fields : []
      this.isIndeterminate1 = false
    },
    handleCheckedKeyChange(value) {
      // console.log(value);
      let checkedCount = value.length
      this.checkExcelAll = checkedCount === this.handleJson_fields.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.handleJson_fields.length
    },
    handleCheckedKeyChange1(value) {
      // console.log(value);
      let checkedCount = value.length
      this.checkExcelAll1 = checkedCount === this.handleMember_fields.length
      this.isIndeterminate1 = checkedCount > 0 && checkedCount < this.handleMember_fields.length
    },
    beforeGenerate() {
      // console.log(e, 'excel');
      this.$message('开始导出文档')
    },
    excelFetch() {
      // console.log('excelFetch', e);
    },
    change(id, statu) {
      this.$confirm('提交后无法修改', '提示', {
        confirmButtonText: '下一步(标记项目)',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning',
        center: true,
      }).then(() => {
        this.showWin = true
        this.reviewForm.id = id
        this.reviewForm.statu = statu
        // this.submitNext(id, statu)
        this.getDataList()
      }).catch((action) => {
        this.getDataList()
        if (action == 'cancel') {
          console.log('cancel');
          this.$message('已取消提交评审')
        }
      })


    },
    test(i) {
      console.log('quxiao', i);
      this.dialogVisible = false
    },
    cancel() {
      this.$message('已取消')
      this.showWin = false
      this.getDataList()
    },
    handleClose() {
      // console.log('handleClose');
      this.showWin = false
      this.getDataList()
    },

    tijiao(id, statu) {
      this.$http({
        url: this.$http.adornUrl('/sys/college/refer'),
        method: 'post',
        params: {
          id,
          statu
        }
      }).then(({
        data
      }) => {
        if (data.code == 0) {
          this.$message.success('提交成功')
          this.getDataList()
        } else {
          this.$message.error('提交失败' + data.msg)
        }
      })
    },

    //提交给校级
    submitNext() {
      this.$http({
        url: this.$http.adornUrl('/sys/college/refer'),
        method: 'post',
        params: {
          ...this.reviewForm
        }
      }).then(({
        data
      }) => {
        if (data.code == 0) {
          this.$message.success('提交成功')
          this.getDataList()
          this.reviewForm.ranks = '一般项目'
          this.reviewForm.id = null
          this.reviewForm.statu = null
          // this.dialogVisible = false
          this.showWin = false
        } else {
          this.$message.error('提交失败' + data.msg)
        }
      })

    },
    //导出项目信息excel
    downExcel() {
      var ids = this.dataListSelections.map(item => {
        return item.id
      })
      this.$http({
        url: this.$http.adornUrl('/sys/college/downexcel'),
        method: 'post',
        data: ids
      }).then(({
        data
      }) => {
        // console.log(data);
        if (data && data.code === 0) {
          this.excelData = data.collegeEntityList
        } else {
          this.$message('数据错误，请联系管理员')
        }

      })
    },
    //导出项目成员信息excel
    downExcelOfMember() {
      var ids = this.dataListSelections.map(item => {
        return item.id
      })
      this.$http({
        url: this.$http.adornUrl('/sys/college/outExcel'),
        method: 'post',
        data: ids
      }).then(({
        data
      }) => {
        this.memberData = data.studentEntities
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/college/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key,
          // 'userId':
        })
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.$message.error(data.msg)
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      this.excelData = val
      if (this.dataListSelections.length != 0) {
        this.downExcel()
        this.downExcelOfMember()

      }
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/college/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    isEmtpy() {
      if (this.excelData.length == 0) {
        this.$message.warning('请先选择要导出的项目')
      }
    },
    toDetail(val) {
      console.log(val)
      this.$router.push({ name: 'project_detail1', query: { id: val } })
    }
  }
}
</script>


<style lang="scss">
.getExcel {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  // background-color: #000;
  // justify-content: space-between;
  // margin: 10px 0;
  background-color: #fff;
  border-radius: 15px;
  padding: 0px 20px;
  box-sizing: border-box;

  .excelForm {
    // display: flex;
    // padding: 10px 0;
    // height: 60px;
    // background-color: #f2f3f5;
    box-sizing: border-box;
    width: 600px;

  }
}
</style>
